name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version'
        required: true

env:
  JAVA_VERSION: '22'
  JAVA_DISTRO: 'adopt'

jobs:
  precheck:
    name: Precheck
    runs-on: ubuntu-latest
    outputs:
      VERSION: ${{ steps.vars.outputs.VERSION }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Version
        id: vars
        shell: bash
        run: |
          VERSION=${{ github.event.inputs.version }}
          ./mvnw -Ppublication -B versions:set versions:commit -DnewVersion=$VERSION
          git config --global user.name "GitHub Action"
          git commit -a -m "Releasing version $VERSION"
          git push origin main

  release:
    needs: [ precheck ]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: main
          fetch-depth: 0

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          java-version: ${{ env.JAVA_VERSION }}
          distribution: ${{ env.JAVA_DISTRO }}

      - name: Cache Maven
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2

      - run: sudo apt-get update && sudo apt-get install -y --no-install-recommends clang libbpf-dev linux-tools-common linux-tools-$(uname -r) gcc-multilib

      - name: Build
        run: ./mvnw -ntp -B --file pom.xml verify

      - name: Build
        run: ./mvnw -Ppublication -ntp -B --file pom.xml verify

      - name: Deploy
        run: |
          mkdir -p ~/.m2
          echo $SETTINGS_FILE > ~/.m2/settings.xml
          mvn -ntp -B clean source:jar deploy
        env:
          MAVEN_USERNAME: ${{ secrets.JRELEASER_NEXUS2_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.JRELEASER_NEXUS2_PASSWORD }}
          SIGN_KEY_PASS: ${{ secrets.JRELEASER_GPG_PASSPHRASE }}
          SIGN_KEY: ${{ secrets.JRELEASER_GPG_SECRET_KEY }}
          GPG_PUBLIC_KEY: ${{ secrets.JRELEASER_GPG_PUBLIC_KEY }}
          SIGN_KEY_ID: ${{ secrets.GPG_KEY_NAME }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SETTINGS_FILE: ${{ secrets.SETTINGS_FILE }}